package com.metacube.metaconnect.model;

import java.io.InputStream;
import java.sql.*;
import java.util.Scanner;

public class CreateConnectionToDatabase {

	/**
	 * creates a connection to database
	 * 
	 * @return Connection class object for interacting with database
	 */
	public static Connection createConnection() {

		Connection conn = null;
		try {

			Scanner file = null;

			// reading file to get driver, connectionString,userName and paaword
			// of Mysql
			InputStream in = CreateConnectionToDatabase.class
					.getResourceAsStream("database.txt");

			file = new Scanner(in);

			String driver = file.nextLine();
			String connectionString = file.nextLine();
			String USER = file.nextLine();
			String PASS = file.nextLine();

			// loading the driver
			Class.forName(driver);

			// creating a connection
			conn = DriverManager.getConnection(connectionString, USER, PASS);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// returns the connection
		return conn;
	}

}
